/**
 * @(#)ImageManager.java, 2015年4月8日. Copyright 2012 Yodao, Inc. All rights
 * reserved. YODAO PROPRIETARY/CONFIDENTIAL. Use is
 * subject to license terms.
 */
package com.scitc.pleasurevideo.imgutil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.ColorInt;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
/**
 * @author 针对图片的管理
 */
public class ImageManager {

    public static void bindImage(final Context context,final ImageView image, final String url) {
        if (image == null || url == null) {
            return;
        }

        GlideApp.with(context).load(url).into(image);
        // when complete image.setTag(url);
    }

    // 可指定圆角
    public static void bindSpecialCircleImage(final Context context,final ImageView image, final String url, int radius, int cornerPos) {
        if (image == null || url == null) {
            return;
        }

        RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(radius, 0));
        GlideApp.with(context).load(url).apply(options)
                .into(image);
        // complete image.setTag(url);

    }

    public static void bindAnchorImage(final Context context,final ImageView image, final String url) {
        bindImage(context,image, url);
    }

    public static void bindCircleCenterCropImage(final Context context,final ImageView image, final String url, int radius) {
        if (image == null || url == null) {
            return;
        }
        GlideApp.with(context).load(url).apply(new RequestOptions().circleCrop())
                .into(image);
    }

    public static void bindCircleImageCenterCrop(final Context context,final ImageView image, final String url) {
        if (image == null || url == null) {
            return;
        }
        GlideApp.with(context).load(url).apply(new RequestOptions().circleCrop())
                .into(image);
    }

    public static void bindCircleImageCenterCrop(final Context context,final ImageView image, final int drawableId) {
        if (image == null) {
            return;
        }
        GlideApp.with(context).load(drawableId).apply(new RequestOptions().circleCrop())
                .into(image);
    }

    public static void bindCircleImage(final Context context,final ImageView image, final String url, int radius) {
        if (image == null || url == null) {
            return;
        }

        RequestOptions options = new RequestOptions()
                .transform(new GlideRoundTransform(radius, 0));
        GlideApp.with(context).load(url).apply(options)
                .into(image);
        // complete image.setTag(url);
    }

    public static void bindCircleImageCenterCrop(final Context context,final ImageView image, final String url, int radius,
                                                 float widthHeightRatio) {
        if (image == null || url == null) {
            return;
        }
        RequestOptions options = new RequestOptions()
                .transform(new GlideRoundTransform(radius, 0));
        GlideApp.with(context).load(url).apply(options)
                .into(image);
    }

    public static void bindImageWithDefault(final Context context,ImageView image, String url, int imageResId) {

        GlideApp.with(context)
                .load(url)
                .apply(new RequestOptions().placeholder(imageResId))
                .into(image);
    }

    /**
     * 加载中间裁剪的圆角图片
     *
     * @param image
     * @param url
     * @param radius 单位dp
     */
    public static void bindRoundedCornersImageCenterCrop(final Context context,final ImageView image, final String url, int radius) {
        if (image == null || url == null) {
            return;
        }

        RequestOptions options = new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(DensityUtil.dip2px(context, radius)));
        GlideApp.with(context).load(url).apply(options)
                .into(image);
    }

    /**
     * 加载中间裁剪的圆角图片，带加载错误图片
     *
     * @param image
     * @param url
     * @param radius 单位dp
     */
    public static void bindRoundedCornersImageCenterCrop(final Context context,final ImageView image, final String url, int errorHolder, int radius) {
        if (image == null) {
            return;
        }

        RequestOptions options = new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(DensityUtil.dip2px(context, radius))).error(errorHolder);
        GlideApp.with(context).load(url).apply(options)
                .into(image);
    }

    /**
     * 加载中间裁剪的圆角图片,加载本地图片
     *
     * @param image
     * @param radius 单位dp
     */
    public static void bindRoundedCornersImageCenterCrop(final Context context,final ImageView image, int resId, int radius) {
        if (image == null) {
            return;
        }

        RequestOptions options = new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(DensityUtil.dip2px(context, radius)));
        GlideApp.with(context).load(resId).apply(options).into(image);
    }

    /**
     * 在高斯模糊后再覆盖一个不透明为0x29000000的黑色图片的实现代码
     * 加载高斯模糊图片
     */
    public static void bindBlurImageCenterCrop(final Context context,ImageView imageview, String imgUrl, int blurRadius, @ColorInt int colorBlur) {


        if (imageview == null) {
            return;
        }
        RequestOptions options = new RequestOptions().transforms(new BlurTransformation(blurRadius, 1), new ColorFilterTransformation(context, colorBlur));
        GlideApp.with(context).load(imgUrl).apply(options).into(imageview);
    }


    // 将Drawable转化为Bitmap
    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }

     public static void bindImageCenterCrop(final Context context,final ImageView image, final String url) {
        if (image == null || url == null) {
            return;
        }
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop());
        GlideApp.with(context).load(url).apply(options)
                .into(image);
    }

}
